import time
from machine import Pin, RTC
import network
import ntptime

led = Pin(2, Pin.OUT)

class Clock:
    """
    定义可以用来控制32x16的点阵屏类
    """
    def __init__(self, wifi, password):
        self.se = 0
        self.rtc = RTC()
        self.wifi = wifi
        self.password = password
        self.ntp()  # 调用这个方法来联网设置时间
        self.second_light = False
       
    def connect_wifi(self):
        wlan = network.WLAN(network.STA_IF)
        wlan.active(True)
        if not wlan.isconnected():
            wlan.connect(self.wifi, self.password)
            i = 1
            while not wlan.isconnected():
                print("正在链接...{}".format(i))
                i += 1
                time.sleep(1)
        print('network config:', wlan.ifconfig())
    
    def ntp(self):
        self.connect_wifi()
        time.sleep(2)
        ntptime.host="ntp1.aliyun.com"
        ntptime.NTP_DELTA = 3155644800  # 东八区 UTC+8偏移时间（秒）
        try:
            ntptime.settime()
            print("联网成功...")
        except Exception as e:
            pass

    def show_time(self):
        # 获取真正的时间
        date = self.rtc.datetime()
        hour = date[4]
        minute = date[5]
        print("minute:",minute)
        print("hour:",hour)
        if hour == 8 and minute >= 34:
            for i in range (10):
                led.value(1)
                time.sleep(0.5)
                led.value(0)
                time.sleep(0.5)
            time.sleep(30) 
   
# 1. 创建对象
clock = Clock("303", "303303303")

# 2. 调用显示

# while True:
#     clock.show_time()
#     time.sleep(1)

